<template>
  <n-layout has-sider style="height: 100vh">
    <n-layout-sider
      bordered
      collapse-mode="width"
      :collapsed-width="64"
      :width="240"
      :collapsed="collapsed"
      show-trigger
      @collapse="handleCollapse"
      @expand="handleExpand"
    >
      <div class="logo" :class="{ collapsed }">
        <span v-show="!collapsed">路人新闻管理后台</span>
        <span v-show="collapsed">路人</span>
      </div>
      <SideMenu />
    </n-layout-sider>
    <n-layout>
      <n-layout-header bordered style="height: 64px; padding: 0 24px; display: flex; align-items: center; justify-content: space-between;">
        <div class="header-left">
          <div class="header-left-content">
            <n-button quaternary @click="toggleSidebar">
              <template #icon>
                <n-icon><Icons name="menu-outline" /></n-icon>
              </template>
            </n-button>
            <!-- Tab导航放在左侧 -->
            <div class="tab-wrapper">
              <TabNav />
            </div>
          </div>
        </div>
        <div class="header-right">
          <n-space>
            <ThemePicker />
            <n-dropdown :options="languageOptions" @select="handleLanguageSelect">
              <n-button quaternary>
                <template #icon>
                  <n-icon><Icons name="language-outline" /></n-icon>
                </template>
                {{ currentLanguage }}
              </n-button>
            </n-dropdown>
            <n-dropdown :options="userMenuOptions" @select="handleUserMenuSelect">
              <n-button quaternary>
                <template #icon>
                  <n-icon><Icons name="person-outline" /></n-icon>
                </template>
                {{ authStore.nickname || authStore.username }}
              </n-button>
            </n-dropdown>
          </n-space>
        </div>
      </n-layout-header>
      <n-layout-content content-style="padding: 24px">
        <router-view />
      </n-layout-content>
    </n-layout>
  </n-layout>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { useAppStore } from '@/store'
import { useAuthStore } from '@/store/auth'
import { 
  NLayout, 
  NLayoutSider, 
  NLayoutHeader, 
  NLayoutContent,
  NButton,
  NIcon,
  NSpace,
  NDropdown,
  useMessage
} from 'naive-ui'
import Icons from './Icons.vue'
import SideMenu from './SideMenu.vue'
import ThemePicker from './ThemePicker.vue'
import TabNav from './TabNav.vue'

const router = useRouter()
const { locale } = useI18n({ useScope: 'global' })
const message = useMessage()
const appStore = useAppStore()
const authStore = useAuthStore()

const collapsed = computed(() => appStore.collapsed)

const currentLanguage = computed(() => {
  return locale.value === 'zh' ? '中文' : 'English'
})

const languageOptions = [
  { label: '中文', key: 'zh' },
  { label: 'English', key: 'en' }
]

const userMenuOptions = [
  { 
    label: `用户：${authStore.username}`,
    key: 'userInfo',
    disabled: true
  },
  { 
    label: `角色：${authStore.getRoleName}`,
    key: 'roleInfo',
    disabled: true
  },
  {
    type: 'divider',
    key: 'divider'
  },
  {
    label: '退出登录',
    key: 'logout'
  }
]

const handleCollapse = () => {
  appStore.toggleSidebar()
}

const handleExpand = () => {
  appStore.toggleSidebar()
}

const toggleSidebar = () => {
  appStore.toggleSidebar()
}

const handleLanguageSelect = (key: string) => {
  const lang = key as 'zh' | 'en'
  locale.value = lang
  appStore.setLocale(lang)
}

const handleUserMenuSelect = async (key: string) => {
  switch (key) {
    case 'logout':
      const result = await authStore.logout()
      if (result.success) {
        message.success(result.message)
        router.push('/login')
      } else {
        message.error(result.message)
      }
      break
  }
}
</script>

<style scoped>
.logo {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 18px;
  border-bottom: 1px solid var(--border-color);
  background-color: var(--bg-color);
  color: var(--text-color);
  transition: all 0.3s;
}

.logo.collapsed {
  font-size: 16px;
}

.header-left {
  display: flex;
  align-items: center;
  flex: 1;
  min-width: 0;
}

.header-right {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.header-left-content {
  display: flex;
  align-items: center;
  gap: 16px;
  width: 100%;
  min-width: 0;
}

.tab-wrapper {
  flex: 1;
  max-width: calc(100vw - 400px); /* 为右侧按钮留出空间 */
  overflow: hidden;
}

.tab-wrapper :deep(.tab-nav) {
  height: 40px;
  background-color: transparent;
  border-bottom: none;
}

.tab-wrapper :deep(.tab-list) {
  align-items: center;
  justify-content: flex-start;
}

.tab-wrapper :deep(.tab-item) {
  height: 32px;
  margin: 4px 2px;
}
</style>